package com.loren.demo09.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.concurrent.TimeUnit;

@Slf4j
@Configuration
@EnableScheduling
public class CaffeineConfig {


    @Bean
    public Cache<String, String> fixedTimeCacheMachine() {
        return Caffeine.newBuilder()
                .expireAfterWrite(10, TimeUnit.SECONDS)
                .removalListener((key, value, removalCause) -> log.info("remove cache, key = {}, value = {}", key, value))
                .build();
    }

    @Bean
    public Cache<String, String> sessionCacheMachine() {
        return Caffeine.newBuilder()
                .expireAfterAccess(10, TimeUnit.SECONDS)
                .removalListener((key, value, removalCause) -> log.info("remove cache, key = {}, value = {}", key, value))
                .build();
    }
}
